import Vue from 'vue'
import axios from 'axios';
import {serialize, getSession,getLocalStorage,getSessionStorage} from './storage';
import {SysData} from './sysData';
import {headers,postFileHeaders} from './config';
import { Message } from 'iview';
import * as FileSaver from "file-saver";
export function request(params) {
    // POST请求（参数、返回值类型都是任意类型）
    if (params['method'] === 'post' || params['method'] === 'POST') {
      return this.postInfo(params['url'], params['data']);
    } else { // 其他请求
      return this.getInfo(params['url'], params['data']);
    }
}

//导出文件
export function exportExcel(url, params,fileName){
    let args=JSON.stringify(params).replace(/\"/g,'%22').replace(/\{/g,'%7b').replace(/\}/g,'%7d')
    var xhr = new XMLHttpRequest();//创建新的XHR对象
    xhr.open('POST', url+'?args='+args);//指定获取数据的方式和url地址
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
    xhr.responseType = 'blob';//以blob的形式接收数据，一般文件内容比较大
    xhr.onload = function(e) {
        var blob = this.response;//Blob数据
        if (this.status == 200) {
            if (blob && blob.size > 0) {
                return FileSaver.saveAs(blob,fileName);
            }
        }
    };
    xhr.send(args); //post请求传的参数
}
export function postInfo(url,params){
    let login = getSessionStorage("LoginForm");
    if(params){
        if(!params.LoginForm){
            params['LoginForm'] = JSON.stringify(login);
        }
    } else {
        params = {LoginForm:JSON.stringify(login)};
    }
    let pa = JSON.stringify(params).replace(/\%/g,"%25").replace(/\+/g, "%2B").replace(/\//g,"%2F").replace(/\?/g,"%3F").replace(/\&/g,"%26").replace(/\=/g,"%3D").replace(/\#/g,"%23");
    return axios.post(url,'args='+pa,{
        headers:headers.headers
    }).then((res) => {
        return Promise.resolve(res.data);
    }).catch(function (error) {
        handleError(error);
    })
}
export function getInfo (url,params) {
    let login = getSessionStorage("LoginForm");
    if(params){
        if(!params.LoginForm){
            params['LoginForm'] = JSON.stringify(login);
        }
    } else {
        params = {LoginForm:JSON.stringify(login)};
    }
    let pa = JSON.stringify(params).replace(/\%/g,"%25").replace(/\+/g, "%2B").replace(/\//g,"%2F").replace(/\?/g,"%3F").replace(/\&/g,"%26").replace(/\=/g,"%3D").replace(/\#/g,"%23");
    return axios.get(url,'args='+pa,{
        headers:headers.headers
    }).then((res) => {
        return Promise.resolve(res.data);
    }).catch(function (error) {
        handleError(error);
    })
}
//带文件的请求
export function postFile(url,data){
    return axios.post(url, data,postFileHeaders).then((res) => {
        return Promise.resolve(res.data);
      }).catch((error)=> {
        handleError(error);
      })
}
export function handleError(error) {
    let errMsg = "网络连接错误，请联系管理员！";
    if (error.status === 404) {
      errMsg = '网络连接错误，请确认当前应用服务器是否正常运行！';
    } else if (error.status === 500) {
      errMsg = '请求的服务器错误';
    }else if (error.status === 504) {
      errMsg = '网络连接错误，请确认当前应用服务器是否正常运行！';
    }
    Message.error(errMsg);
  }

export function post(url,params){
  let pa = JSON.stringify(params);
  return axios.post(url,pa,{
    headers:headers.headers
  }).then((res) => {
    return Promise.resolve(res.data);
  }).catch(function (error) {
    handleError(error);
  })
}

export function postTestEas(url,params,langType){
  let pa = JSON.stringify(params);
  headers.headers.lang=langType;
  let head = headers.headers;
    console.log("传入参数");
  console.log(pa);
  console.log(head);
  return axios.post(url,'args='+pa,{
    headers:head
  }).then((res) => {
    return Promise.resolve(res.data);
  }).catch(function (error) {
    handleError(error);
  })
}
